﻿/**
 * http://gold.ncfdc.com.cn/Default.aspx?tname=251\main
 * 南昌领航房产信息爬虫
 * sunny/acc 2017-06-20
 */

const utils = require("../../utils");
import { Base, Item } from "../base";

class TempClass {
    name: string;
    url: string;
}

class ncfdc_spider extends Base {
    tmps: TempClass[];
    currentPageContent: string;

    constructor() {
        super();
        this.name = "南昌领航房产信息[扩展]";
        this.encode = "utf-8";
        this.tmps = [];
        this.timeout = 1000000;
        this.debug = false;
        this.collection = "ncfdcxxext";
    }

    getPageCount(): number {
        var self = this;
        var content: string = this.spider.getSync("http://gold.ncfdc.com.cn/Default.aspx?tname=251\\main");
        var $: CheerioSelector = this.cheerio.load(content);
        var cnt: number = 0;
        var index = 0;
        $("div.lpda_pinyin a").each(function (index, ele) {
            cnt++;
            var url: string = $(ele).attr("href");
            var name: string = $(ele).text().trim();
            var item: TempClass = new TempClass();
            item.name = name;
            item.url = url;
            self.tmps.push(item);
            index++;
            console.log(`load item ${index} ${name}`);
        });
        return cnt;
    }

    getItems(pageIndex: number): Item[] {
        var result: Item[] = [];
        var item: TempClass = this.tmps[pageIndex];
        var url: string = "http://gold.ncfdc.com.cn/" + item.url;
        var content: string = this.spider.getSync(url);
        var $: CheerioSelector = this.cheerio.load(content);
        try {
            var kfs = $("#ctl20_developer_name").text().trim();
            var suburl = $(".doc_xq_xsxx_A3").attr("href").trim();
            if (suburl) {
                this.processList(item.name, suburl, kfs, result);
            }
            return result;
        } catch (e) {
            return [];
        }
    }

    // 获取房管局信息
    private processList(title: string, suburl: string, kfs: string, items: Item[]): void {
        var content = this.spider.getSync(suburl);
        this.currentPageContent = content;
        var $ = this.cheerio.load(content);
        var pgCount: number = 1;
        pgCount = parseInt($("#ctl16_grdSeatList_ctl23_lblPageCount").text().trim());
        // 当pgCount为NaN的时候说明没有分页，则pgCount默认取值为1
        if (isNaN(pgCount)) {
            this.processPage(1, title, suburl, kfs, items);
        } else {
            for (var i = 1; i <= pgCount; i++) {
                this.processPage(i, title, suburl, kfs, items);
            }
        }

    }

    // 获取某页的信息
    private processPage(pageIndex: number, title: string, suburl: string, kfs: string, items: Item[]): void {
        if (pageIndex == 1) {
            // 当页码为1的时候，直接使用，不需要跳转
        } else {
            var form: any = {};
            var $ = this.cheerio.load(this.currentPageContent);
            $("#form1 input[type='hidden']").each(function (index, ele) {
                var e = $(ele);
                var v = e.attr("value");
                var n = e.attr("name");
                if (v) {
                    form[n] = v;
                }
                form["__EVENTTARGET"] = "ctl16$grdSeatList$ctl14$cmdPreview";
                form["__EVENTARGUMENT"] = "";
                form["__VIEWSTATEENCRYPTED"] = "";
                form["ctl16$txtSearch"] = "";
            });
            this.currentPageContent = this.spider.postSync(suburl, form); // POST跳转方式F
        }
        var $ = this.cheerio.load(this.currentPageContent);
        var trs = $("#ctl16_grdSeatList tr");
        var trslength = trs.length;
        var self = this;
        trs.each(function (index, ele) {
            if (index > 1 && (index < trslength - 1)) { // 忽略前2行
                var ref = $(ele);
                var ldname = ref.find("td").eq(0).text().trim(); // 楼栋名称
                var ysxkz = ref.find("td").eq(1).text().trim(); // 预售证号
                var fzrq = ref.find("td").eq(2).text().trim(); // 发证日期
                var _url = ref.find("td").eq(8).find("a").attr("href");
                self.processItems(title, ldname, ysxkz, fzrq, _url, kfs, items);
            }
        });
    }

    // 获取最后一级的明细数据
    private processItems(title: string, ldname: string, ysxkz: string, ysrq: string, url: string, kfs: string, items: Item[]): void {
        var content = this.spider.getSync(url);
        var $ = this.cheerio.load(content);
        var self = this;

        //console.log("url:" + url);

        var residence_Area: any = [];//住宅面积
        residence_Area[0] = 0.0;//已备案
        residence_Area[1] = 0.0;//已登记权属
        residence_Area[2] = 0.0;//其它不可售房
        residence_Area[3] = 0.0;//可预售
        residence_Area[4] = 0.0;//自留房
        residence_Area[5] = 0.0;//已签合同
        residence_Area[6] = 0.0;//商品房退房上市

        var residence_Price: any = [];//住宅价格
        residence_Price[0] = 0.0;//已备案
        residence_Price[1] = 0.0;//已登记权属
        residence_Price[2] = 0.0;//其它不可售房
        residence_Price[3] = 0.0;//可预售
        residence_Price[4] = 0.0;//自留房
        residence_Price[5] = 0.0;//已签合同
        residence_Price[6] = 0.0;//商品房退房上市

        var nonResidential_Area: any = [];//非住宅面积
        nonResidential_Area[0] = 0.0;//已备案
        nonResidential_Area[1] = 0.0;//已登记权属
        nonResidential_Area[2] = 0.0;//其它不可售房
        nonResidential_Area[3] = 0.0;//可预售
        nonResidential_Area[4] = 0.0;//自留房
        nonResidential_Area[5] = 0.0;//已签合同
        nonResidential_Area[6] = 0.0;//商品房退房上市

        var nonResidential_Price: any = [];//非住宅价格
        nonResidential_Price[0] = 0.0;//已备案
        nonResidential_Price[1] = 0.0;//已登记权属
        nonResidential_Price[2] = 0.0;//其它不可售房
        nonResidential_Price[3] = 0.0;//可预售
        nonResidential_Price[4] = 0.0;//自留房
        nonResidential_Price[5] = 0.0;//已签合同
        nonResidential_Price[6] = 0.0;//商品房退房上市

        $("table.fk_TABLE").each(function (index, ele) {
            var dyh = $(ele).find("tr").eq(2).text().trim(); //单元号
            $(ele).find("tr").each(function (i, el) {
                if (i > 3) {
                    var _item = $(el);
                    var tds = _item.find("td");
                    var outItem = self.buildItem();
                    (<any>outItem)["所在楼层"] = tds.eq(0).text().trim();
                    var fh = tds.eq(1).text().trim();//房号
                    (<any>outItem)["房号"] = fh;
                    var hx = tds.eq(2).text().trim();//户型
                    var hx_length = hx.split("0");//用0来判断是否为住宅和店面
                    (<any>outItem)["户型"] = hx;
                    var jz_Area_Str = tds.eq(3).text().trim();//建筑面积
                    var jz_Area = parseFloat(jz_Area_Str);
                    (<any>outItem)["建筑面积"] = jz_Area;
                    (<any>outItem)["套内面积"] = tds.eq(4).text().trim();
                    (<any>outItem)["公摊面积"] = tds.eq(5).text().trim();
                    var xs_State = tds.eq(6).text().trim();//销售状态
                    (<any>outItem)["销售状态"] = xs_State;
                    (<any>outItem)["朝向"] = tds.eq(7).text().trim();
                    var xs_Price_Str = tds.eq(8).text().trim();//销售参考单价
                    var xs_Price = 0;
                    if (xs_Price_Str) {//如果销售参考单价为空白则默认是0，不然会打印NaN
                        xs_Price = parseInt(xs_Price_Str);
                    }
                    (<any>outItem)["销售参考单价"] = xs_Price;
                    (<any>outItem)["开发商"] = kfs;
                    (<any>outItem)["楼栋名称"] = ldname;
                    (<any>outItem)["预售许可证"] = ysxkz;
                    (<any>outItem)["发证日期"] = ysrq;
                    (<any>outItem)["项目名称"] = title;
                    if (hx_length.length >= 4) {//表示为非住宅
                        if (xs_State.split("已备案").length == 2) {
                            nonResidential_Area[0] += jz_Area;
                            nonResidential_Price[0] += xs_Price;
                        } else if (xs_State.split("已登记权属").length == 2) {
                            nonResidential_Area[1] += jz_Area;
                            nonResidential_Price[1] += xs_Price;
                        } else if (xs_State.split("其它不可售房").length == 2) {
                            nonResidential_Area[2] += jz_Area;
                            nonResidential_Price[2] += xs_Price;
                        } else if (xs_State.split("可预售").length == 2) {
                            nonResidential_Area[3] += jz_Area;
                            nonResidential_Price[3] += xs_Price;
                        } else if (xs_State.split("自留房").length == 2) {
                            nonResidential_Area[4] += jz_Area;
                            nonResidential_Price[4] += xs_Price;
                        } else if (xs_State.split("已签合同").length == 2) {
                            nonResidential_Area[5] += jz_Area;
                            nonResidential_Price[5] += xs_Price;
                        } else if (xs_State.split("商品房退房上市").length == 2) {
                            nonResidential_Area[6] += jz_Area;
                            nonResidential_Price[6] += xs_Price;
                        }
                    } else {//住宅
                        if (xs_State.split("已备案").length == 2) {
                            residence_Area[0] += jz_Area;
                            residence_Price[0] += xs_Price;
                        } else if (xs_State.split("已登记权属").length == 2) {
                            residence_Area[1] += jz_Area;
                            residence_Price[1] += xs_Price;
                        } else if (xs_State.split("其它不可售房").length == 2) {
                            residence_Area[2] += jz_Area;
                            residence_Price[2] += xs_Price;
                        } else if (xs_State.split("可预售").length == 2) {
                            residence_Area[3] += jz_Area;
                            residence_Area[3] += xs_Price;
                        } else if (xs_State.split("自留房").length == 2) {
                            residence_Area[4] += jz_Area;
                            residence_Area[4] += xs_Price;
                        } else if (xs_State.split("已签合同").length == 2) {
                            residence_Area[5] += jz_Area;
                            residence_Area[5] += xs_Price;
                        } else if (xs_State.split("商品房退房上市").length == 2) {
                            residence_Area[6] += jz_Area;
                            residence_Area[6] += xs_Price;
                        }
                    }
                    var t = dyh + " " + fh; // 新的标题
                    t = t.replace(/\n/g, "").replace(/\r/g, "").replace(/\s+/g, " ");
                    outItem.title = t;
                    items.push(outItem);
                }
            });
        });
    };
}

export = new ncfdc_spider();